package com.cloud.bc.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.enums.CookieConst;
import com.cloud.bc.service.BcSinglesService;
import com.cloud.common.util.CookieUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 推荐页面
 */
@AllArgsConstructor
@Controller
@RequestMapping("recruit")
public class RecruitController extends BaseController {
    private final BcSinglesService bcSinglesService;

    /**
     * 推荐好友
     */
    @GetMapping("")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response,
                              String source) {
        String eid = request.getParameter("eid");
        //如果是登录状态就是打开自己的推荐页

        UserInfo userInfo = bcUsersService.getUserInfo(request);
        String url = StrUtil.isEmpty(source) ? "/" : "/" + source;

        //1、如果未登录，且eid不为空
        if ((userInfo == null
                || Convert.toInt(userInfo.getId(), 0) == 0)
                && StrUtil.isNotEmpty(eid)
        ) {
            //如果未登录就保存eid的推荐人到cookie，注册的时候要用到eid做推荐人使用
            CookieUtils.setCookie(request, response, CookieConst.eidCookie, eid);
            return new ModelAndView("redirect:" + url);
        }
        if (null != userInfo && Convert.toInt(userInfo.getId(), 0) > 0
                && null != source && source.equals("m")) {
            ModelAndView view = baseView(request, "推荐好友", "m/recruit/index");
            //如果是已经登录就跳转到推荐好友页面.扩大我的单身团
            BcSingles bcSingles = new BcSingles();
            bcSingles.setUserEid(userInfo.getId().toString());
            Page page = new Page();
            page.setSize(15);
            page.setCurrent(1);
            IPage<BcSingles> bcSinglesIPage = bcSinglesService.selectPage(page, bcSingles);
            List<BcSingles> records = bcSinglesIPage.getRecords();
            int size = records.size();
            if (size < 15) {
                for (int i = 0; i < 15 - size; i++) {
                    BcSingles bcSingles1 = new BcSingles();
                    records.add(bcSingles1);
                }
            }
            view.addObject("recruitList", bcSinglesIPage.getRecords());
            view.addObject("recruitCount", bcSinglesIPage.getTotal());
            return view;
        }

        //如果未登录且推荐人ID是空的，就跳转首页

        //如果未登录就保存eid的推荐人到cookie，注册的时候要用到eid做推荐人使用
        return new ModelAndView("redirect:" + url);
//        return view;
    }
}

